/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.action;

import com.myapp.form.SystemeDifferantForm;
import com.myapp.metier.Helper;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author user
 */
public class SystemeDifferantAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String IPSERVEURORDER = "169.254.43.200";

    public class threadTransfertOrderDiffere extends Thread {

        public void run() {
            Helper helper = new Helper();

            int typeOperation = 1 + (int) (Math.random() * 2);
            int compteClient = 1132 + (int) (Math.random() * 76327);
            int quantity = 1 + (int) (Math.random() * 1000);
            int price = 1000 + (int) (Math.random() * 10000);

            helper.addOrderDiffere(typeOperation, compteClient, quantity, price);
        }
    }

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        SystemeDifferantForm formBean = (SystemeDifferantForm) form;
        Integer nbClient = formBean.getNumber();
        Properties properties = new Properties();
        int activationSystem = 1;
        try {
            properties.load(new FileInputStream("C:\\Users\\user\\Documents\\NetBeansProjects\\ordrediffere\\Hibernate\\src\\java\\com\\myapp\\action\\proprieteServer.properties"));
            activationSystem = Integer.valueOf(properties.getProperty("systemeDifferant"));
        } catch (IOException e) {
            System.out.println(e);
        }
        if (activationSystem == 0) {
            URL url = new URL("http://" + IPSERVEURORDER + ":8080/Hibernate/PassageOrdreRegional.do?nbClient=" + nbClient);
            URLConnection yc = url.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            yc.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {

            }
            in.close();
        } else {
            // Mesurer la charge sur le serveur de passage d'ordre

            URL url = new URL("http://" + IPSERVEURORDER + ":8080/Hibernate/MesureCharge.do");
            URLConnection yc = url.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            yc.getInputStream()));
            String inputLine;
            String currentCharge = "";
            while ((inputLine = in.readLine()) != null) {
                currentCharge += inputLine;
            }
            in.close();
            int charge = Integer.valueOf(currentCharge);

            if (charge > 1000) {
                for (int i = 0; i < nbClient; i++) {
                    new threadTransfertOrderDiffere().start();
                    if (i % 200 == 0) {
                        Thread.sleep(500);
                    }
                }

            } else {
                URL urlord = new URL("http://" + IPSERVEURORDER + ":8080/Hibernate/PassageOrdreRegional.do?nbClient=" + nbClient);
                URLConnection ycord = urlord.openConnection();
                BufferedReader inord = new BufferedReader(
                        new InputStreamReader(
                                ycord.getInputStream()));
                String inputLineOrd;

                while ((inputLineOrd = inord.readLine()) != null) {

                }
                inord.close();
            }
        }

        return mapping.findForward(SUCCESS);
    }
}
